'''

Case Type： 控制文件
Case Name： 控制文件无权限
Create At:  2020/07/01
Owner    :  @wan005
Descption:  1.控制文件和备份文件都无读写权限，数据库运行时checkpoint，启动数据库、停止数据库 2.只修改控制文件，不修改备份控制文件

'''
import os
import sys
import unittest
import _thread
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common


class ChmodCtrlfile(unittest.TestCase):
    sh = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case071.py start------------')
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.PG_CONTROL_PATH = macro.PG_CONTROL_PATH
        self.primary_root = Node(node='PrimaryRoot')
        self.com = Common()
        self.Constant = Constant()

    def test_chmod_allctrl(self):
        self.log.info('--------------this is test_chmod_allctrl----------------')
        # 修改控制文件及备份文件的权限
        self.log.info('-------------修改控制文件及备份文件的权限--------------')
        cmd = f'chmod 000 {self.PG_CONTROL_PATH}*'
        self.log.info(cmd)
        msg = self.primary_root.sh(cmd).result()
        self.log.info(msg)

        # 数据库运行时checkpoint
        self.log.info('--------------------------checkpoint--------------------------')
        sql = 'checkpoint;'
        self.log.info(sql)
        msg = self.sh.executDbSql(sql)
        self.log.info(msg)
        fail_checkpoint = msg.find(str(self.Constant.FAILED_CHECKPOINT)) > -1
        self.assertTrue(fail_checkpoint)

        # 停止数据库
        self.log.info('------------停止数据库------------------------')
        status = self.sh.stopDbCluster()
        self.assertTrue(status)

        # 启动数据库
        self.log.info('------------------启动数据库------------------------')
        status = self.sh.startDbCluster(True)
        self.log.info(status)
        self.assertIn(self.Constant.OPEN_FAIL, status)

    def test_chmod_ctrlbak(self):
        self.log.info('--------------this is test_chmod_ctrlbak-----------------')
        # 只修改控制文件不修改备份文件
        self.log.info('-----------------只修改控制文件不修改备份文件-----------------')
        cmd = f'chmod 000 {self.PG_CONTROL_PATH}'
        self.log.info(cmd)
        msg = self.primary_root.sh(cmd).result()
        self.log.info(msg)

        # 数据库运行时checkpoint
        self.log.info('-----------------checkpoint----------------------')
        sql = 'checkpoint;'
        self.log.info(sql)
        msg = self.sh.executDbSql(sql)
        self.log.info(msg)
        fail_checkpoint = msg.find(str(self.Constant.FAILED_CHECKPOINT)) > -1
        self.assertTrue(fail_checkpoint)

        # 停止数据库
        self.log.info('---------------------停止数据库---------------------')
        status = self.sh.stopDbCluster()
        self.assertTrue(status)
        # 启动数据库报错
        self.log.info('--------------启动数据库报错--------------------------')
        status = self.sh.startDbCluster(True)
        self.log.info(status)
        self.assertIn(self.Constant.OPEN_FAIL, status)

    def tearDown(self):
        self.log.info('---------------------this is tearDown--------------------')
        # 清理环境
        self.log.info('------------------恢复文件权限--------------------')
        cmd = f'chmod 600 {self.PG_CONTROL_PATH}*'
        self.primary_root.sh(cmd).result()

        self.log.info('---------------------------启动数据库------------------------------')
        self.sh.startDbCluster()
